package com.sky.interceptors;

import cn.dev33.satoken.stp.StpUtil;
import com.sky.constant.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Component
public class TokenInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("token拦截器工作...");
        String token = request.getHeader("authentication");
        log.info("token:{}", token);
        Object id = StpUtil.getLoginIdByToken(token);
        log.info("id:{}", id);
        Long userId = Long.parseLong(id + "");
        BaseContext.setUserId(userId);
        return true;
    }
}
